﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Web;
using ConsolidationTool.Common;

namespace ConsolidationTool.WebParts.Layouts.AjaxServices
{
    public class ExchangeRateServices : IAjaxService
    {
        RateServices rateServices;

        public AjaxResult Excute(System.Web.HttpContext httpContext, FSSearchEntity se)
        {
            rateServices = new RateServices();

            var date = DateTime.Parse(string.Format("{0}-{1}-{2}", se.Year, se.Month, 1));

            var lastMonthRate = rateServices.LoadExchangeRate(se.ListTitle, date.Year + "", (date.Month - 1) + "");
            var currentMonthRate = rateServices.LoadExchangeRate(se.ListTitle, date.Year + "", date.Month + "");

            var lastMonthRateItem = lastMonthRate.FirstOrDefault(p => p.FC == se.Currency);
            var currentMonthRateItem = currentMonthRate.FirstOrDefault(p => p.FC == se.Currency);

            var lastMonthRateValue = lastMonthRateItem == null ? "0" : lastMonthRateItem.CM;
            var currentMonthRateValue = currentMonthRateItem == null ? "0" : currentMonthRateItem.CM;

            decimal diffValue = 0.00M;
            bool success = true;
            try
            {
                diffValue = BusinessService.decimalParse(lastMonthRateValue) - BusinessService.decimalParse(currentMonthRateValue);
            }
            catch (Exception ex)
            {
                success = false;
            }
            se.Data = diffValue + "";

            return new AjaxResult(success, se.Data) { FullMsg = "Successfull" };
        }
    }
}
